<template>
  <div class="menu-create">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ isEdit ? '编辑广告' : '添加广告' }}</span>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="广告名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="广告位置">
          <el-select v-model="form.spaceId" placeholder="请选择">
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="item in parentMenuList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="上线/下线">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">下线</el-radio>
            <el-radio :label="1">上线</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="广告图片">
          <course-image
            :limit="5"
            v-model="form.img"
          />
        </el-form-item>
        <el-form-item label="广告链接" prop="link">
          <el-input v-model="form.link"></el-input>
        </el-form-item>
        <el-form-item label="广告备注">
          <el-input
            style="margin-bottom: 10px"
            v-model="form.text"
            type="textarea"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button
            v-if="!isEdit"
            @click="onReset"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { Form } from 'element-ui'
import CourseImage from '@/views/course/components/CourseImage.vue'
import { createOrUpdateMenu, getEditMenuInfo, getAdvertInfo } from '@/services/advert'

export default Vue.extend({
  name: 'MenuCreateOrEdit',
  components: {
    CourseImage
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: {
        spaceId: '', // -1 表示没有上级菜单
        name: '',
        startTime: '',
        endTime: '',
        status: 0,
        img: '',
        link: '',
        text: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入广告名称', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '请选择开始时间', trigger: 'change' }
        ],
        endTime: [
          { required: true, message: '请选择到期时间', trigger: 'change' }
        ],
        link: [
          { required: true, message: '请输入广告链接', trigger: 'blur' }
        ]
      },
      parentMenuList: [] // 父级菜单列表
    }
  },

  created () {
    this.loadMenuInfo()

    if (this.isEdit) {
      this.loadAdvertInfo()
    }
  },

  methods: {
    async loadMenuInfo () {
      const { data } = await getEditMenuInfo()
      this.parentMenuList = data.content
    },

    async loadAdvertInfo () {
      const { data } = await getAdvertInfo({ id: this.$route.params.id })
      this.form = data.content
    },

    onReset () {
      (this.$refs.form as Form).resetFields()
    },

    async onSubmit () {
      // 1. 表单验证
      // 2. 验证通过，提交表单
      (this.$refs.form as Form).validate(async (valid: any) => {
        if (valid) {
          const { data } = await createOrUpdateMenu(this.form)
          if (data.success) {
            this.$message.success('提交成功')
            this.$router.back()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
})
</script>

<style lang="scss" scoped></style>
